<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>DSF2FLAC: dsd_sample_reader.cpp Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">DSF2FLAC
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">dsd_sample_reader.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * dsf2flac - http://code.google.com/p/dsf2flac/</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * A file conversion tool for translating dsf dsd audio files into</span>
<a name="l00005"></a>00005 <span class="comment"> * flac pcm audio files.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Copyright (c) 2013 by respective authors.</span>
<a name="l00008"></a>00008 <span class="comment"> *</span>
<a name="l00009"></a>00009 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
<a name="l00010"></a>00010 <span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<a name="l00011"></a>00011 <span class="comment"> * the Free Software Foundation; either version 2 of the License, or</span>
<a name="l00012"></a>00012 <span class="comment"> * (at your option) any later version.</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
<a name="l00015"></a>00015 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00016"></a>00016 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<a name="l00017"></a>00017 <span class="comment"> * GNU General Public License for more details.</span>
<a name="l00018"></a>00018 <span class="comment"> *</span>
<a name="l00019"></a>00019 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
<a name="l00020"></a>00020 <span class="comment"> * along with this program; if not, write to the Free Software</span>
<a name="l00021"></a>00021 <span class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</span>
<a name="l00022"></a>00022 <span class="comment"> *</span>
<a name="l00023"></a>00023 <span class="comment"> *</span>
<a name="l00024"></a>00024 <span class="comment"> * Acknowledgements</span>
<a name="l00025"></a>00025 <span class="comment"> *</span>
<a name="l00026"></a>00026 <span class="comment"> * Many thanks to the following authors and projects whose work has greatly</span>
<a name="l00027"></a>00027 <span class="comment"> * helped the development of this tool.</span>
<a name="l00028"></a>00028 <span class="comment"> *</span>
<a name="l00029"></a>00029 <span class="comment"> *</span>
<a name="l00030"></a>00030 <span class="comment"> * Sebastian Gesemann - dsd2pcm (http://code.google.com/p/dsd2pcm/)</span>
<a name="l00031"></a>00031 <span class="comment"> * SACD Ripper (http://code.google.com/p/sacd-ripper/)</span>
<a name="l00032"></a>00032 <span class="comment"> * Maxim V.Anisiutkin - foo_input_sacd (http://sourceforge.net/projects/sacddecoder/files/)</span>
<a name="l00033"></a>00033 <span class="comment"> * Vladislav Goncharov - foo_input_sacd_hq (http://vladgsound.wordpress.com)</span>
<a name="l00034"></a>00034 <span class="comment"> * Jesus R - www.sonore.us</span>
<a name="l00035"></a>00035 <span class="comment"> *</span>
<a name="l00036"></a>00036 <span class="comment"> */</span>
<a name="l00037"></a>00037 
<a name="l00049"></a>00049 <span class="preprocessor">#include &lt;dsd_sample_reader.h&gt;</span>
<a name="l00050"></a>00050 
<a name="l00057"></a>00057 DsdSampleReader::DsdSampleReader()
<a name="l00058"></a>00058 {
<a name="l00059"></a>00059         bufferLength = defaultBufferLength;
<a name="l00060"></a>00060         isBufferAllocated = <span class="keyword">false</span>;
<a name="l00061"></a>00061 }
<a name="l00062"></a>00062 
<a name="l00069"></a>00069 DsdSampleReader::~DsdSampleReader()
<a name="l00070"></a>00070 {
<a name="l00071"></a>00071         <span class="keywordflow">if</span> (isBufferAllocated)
<a name="l00072"></a>00072                 <span class="keyword">delete</span>[] circularBuffers;
<a name="l00073"></a>00073         isBufferAllocated = <span class="keyword">false</span>;
<a name="l00074"></a>00074 }
<a name="l00075"></a>00075 
<a name="l00086"></a>00086 boost::circular_buffer&lt;dsf2flac_uint8&gt;* DsdSampleReader::getBuffer()
<a name="l00087"></a>00087 {
<a name="l00088"></a>00088         <span class="keywordflow">return</span> circularBuffers;
<a name="l00089"></a>00089 }
<a name="l00090"></a>00090 
<a name="l00097"></a>00097 dsf2flac_uint32 DsdSampleReader::getBufferLength()
<a name="l00098"></a>00098 {
<a name="l00099"></a>00099         <span class="keywordflow">return</span> bufferLength;
<a name="l00100"></a>00100 }
<a name="l00101"></a>00101 
<a name="l00110"></a>00110 <span class="keywordtype">bool</span> DsdSampleReader::setBufferLength(dsf2flac_uint32 b)
<a name="l00111"></a>00111 {
<a name="l00112"></a>00112         <span class="keywordflow">if</span> (b&lt;1) {
<a name="l00113"></a>00113                 errorMsg = <span class="stringliteral">&quot;dsdSampleReader::setBufferLength:buffer length must be &gt;0&quot;</span>;
<a name="l00114"></a>00114                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00115"></a>00115         }
<a name="l00116"></a>00116         bufferLength=b;
<a name="l00117"></a>00117         resizeBuffer();
<a name="l00118"></a>00118         rewind();
<a name="l00119"></a>00119         <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00120"></a>00120 }
<a name="l00121"></a>00121 
<a name="l00128"></a>00128 dsf2flac_float64 DsdSampleReader::getPositionInSeconds()
<a name="l00129"></a>00129 {
<a name="l00130"></a>00130         <span class="keywordflow">return</span> getPosition() / (dsf2flac_float64) getSamplingFreq();
<a name="l00131"></a>00131 }
<a name="l00132"></a>00132 
<a name="l00139"></a>00139 dsf2flac_float64 DsdSampleReader::getPositionAsPercent()
<a name="l00140"></a>00140 {
<a name="l00141"></a>00141         <span class="keywordflow">return</span> 100* getPosition() / (dsf2flac_float64) getLength();
<a name="l00142"></a>00142 }
<a name="l00143"></a>00143 
<a name="l00150"></a>00150 dsf2flac_float64 DsdSampleReader::getLengthInSeconds()
<a name="l00151"></a>00151 {
<a name="l00152"></a>00152         <span class="keywordflow">return</span> getLength() / (dsf2flac_float64) getSamplingFreq();
<a name="l00153"></a>00153 }
<a name="l00154"></a>00154 
<a name="l00161"></a>00161 <span class="keywordtype">bool</span> DsdSampleReader::isValid()
<a name="l00162"></a>00162 {
<a name="l00163"></a>00163         <span class="keywordflow">return</span> valid;
<a name="l00164"></a>00164 }
<a name="l00165"></a>00165 
<a name="l00172"></a>00172 std::string DsdSampleReader::getErrorMsg()
<a name="l00173"></a>00173 {
<a name="l00174"></a>00174         <span class="keywordflow">return</span> errorMsg;
<a name="l00175"></a>00175 }
<a name="l00176"></a>00176 
<a name="l00183"></a>00183 <span class="keywordtype">void</span> DsdSampleReader::allocateBuffer()
<a name="l00184"></a>00184 {
<a name="l00185"></a>00185         <span class="keywordflow">if</span> (isBufferAllocated)
<a name="l00186"></a>00186                 <span class="keywordflow">return</span>;
<a name="l00187"></a>00187                 
<a name="l00188"></a>00188         circularBuffers = <span class="keyword">new</span> boost::circular_buffer&lt;dsf2flac_uint8&gt; [getNumChannels()];
<a name="l00189"></a>00189         <span class="keywordflow">for</span> (dsf2flac_uint32 i = 0; i&lt;getNumChannels(); i++) {
<a name="l00190"></a>00190                 boost::circular_buffer&lt;dsf2flac_uint8&gt; cb(getBufferLength());
<a name="l00191"></a>00191                 circularBuffers[i] = cb;
<a name="l00192"></a>00192         }
<a name="l00193"></a>00193         isBufferAllocated = <span class="keyword">true</span>;
<a name="l00194"></a>00194         clearBuffer();
<a name="l00195"></a>00195         <span class="keywordflow">return</span>;
<a name="l00196"></a>00196 }
<a name="l00197"></a>00197 
<a name="l00204"></a>00204 <span class="keywordtype">void</span> DsdSampleReader::clearBuffer() 
<a name="l00205"></a>00205 {
<a name="l00206"></a>00206         <span class="keywordflow">if</span> (!isBufferAllocated) {
<a name="l00207"></a>00207                 allocateBuffer();
<a name="l00208"></a>00208                 <span class="keywordflow">return</span>;
<a name="l00209"></a>00209         }
<a name="l00210"></a>00210         
<a name="l00211"></a>00211         dsf2flac_uint8 c = getIdleSample();
<a name="l00212"></a>00212         <span class="keywordflow">for</span> (dsf2flac_uint32 i = 0; i&lt;getNumChannels(); i++)
<a name="l00213"></a>00213                 <span class="keywordflow">for</span> (dsf2flac_uint32 j=0; j&lt;getBufferLength(); j++)
<a name="l00214"></a>00214                         circularBuffers[i].push_front(c);
<a name="l00215"></a>00215 
<a name="l00216"></a>00216 }
<a name="l00217"></a>00217 
<a name="l00224"></a>00224 <span class="keywordtype">void</span> DsdSampleReader::resizeBuffer() {
<a name="l00225"></a>00225         <span class="keywordflow">if</span> (!isBufferAllocated) {
<a name="l00226"></a>00226                 allocateBuffer();
<a name="l00227"></a>00227                 <span class="keywordflow">return</span>;
<a name="l00228"></a>00228         }
<a name="l00229"></a>00229         <span class="keywordflow">for</span> (dsf2flac_uint32 i = 0; i&lt;getNumChannels(); i++)
<a name="l00230"></a>00230                 circularBuffers[i].set_capacity(getBufferLength());
<a name="l00231"></a>00231         clearBuffer();
<a name="l00232"></a>00232 }
<a name="l00233"></a>00233 
<a name="l00234"></a>00234 
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Fri Sep 13 2013 14:42:01 for DSF2FLAC by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
